﻿using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NQueries.TestSupport;
using Rhino.Mocks;

namespace NQueries.Data.Test
{
    [TestClass]
    public class DataContextExtensionsTest
    {
        public TestContext TestContext { get; set; }

        [TestInitialize]
        public void TestInitialize() { MockTools.Reset(); }

        [TestMethod]
        public void GetByIdTest()
        {
            IDataContext dataContext = MockTools.Strict<IDataContext>();

            Type type = typeof(object);
            object id = 42;
            object expected = new object();

            using (MockTools.Record())
                Expect.Call(dataContext.Find(type, id)).Return(expected);

            using (MockTools.Playback())
            {
                object actual = dataContext.GetById(type, id);
                Assert.AreEqual(expected, actual);
            }
        }
        [TestMethod]
        public void GetByIdFailedTest()
        {
            IDataContext dataContext = MockTools.Strict<IDataContext>();

            Type type = typeof(object);
            object id = 42;

            using (MockTools.Record())
                Expect.Call(dataContext.Find(type, id)).Return(null);

            using (MockTools.Playback())
                TestTools.AssertThrows<NQueriesException>(() => dataContext.GetById(type, id));
        }
    }
}